package com.suemi.missile.common.entity;

import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.charset.Charset;

import org.apache.commons.io.IOUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

public class Patent implements Documentable {

  public static String claimPath = "/Volumes/Transcend/20170413101141478-1/claims";


  public String publicationNumber ;

  public String applicationNumber ;

  public String applicationYear ;

  public String patentRype ;

  public String legalStatus ;

  public String applicant ;

  public String proposer ;

  public String currentApplicant;

  public String patentName  ;

  public String IPC;



  public String applicationData ;

  public String openYear ;

  public String openDay ;

  public String patentAbstract ;

  public String independentClaims ;

  public String primaryClaim  ;

  public String claim ;

  public String INPADOC;

  public String simpleLegalState   ;

  public String legalStatusSate ;



  public String inventor  ;

  public String theInventor ;

  public String firstInventor ;

  public String inventorCount ;

  public String applicantCount  ;

  public String applicanProvince   ;

  public String INPADOCCount ;

  public String patentCount;

  public String INPADOCPatent   ;

  public String patentFamily  ;


  public String citationPatternCount ;

  public String documentNotPatternCount ;

  public String beCitedPatternCount ;

  public String citePattern ;

  public String INPADOCCitedTotal;

  public String citedTotal  ;

  public String citedTotalOneYear ;

  public String citedTotalFiveYear;

  public String citedPatent   ;

  public String IPCMainNumber ;

  public String IPCMainNumberBigC ;

  public String IPCMainNumberSmallC ;

  public String IPCMainNumberBigO ;

  public String priorityCountry  ;

  public String priorityNumber  ;

  public String priorityData  ;



  @Override
  public String documentId() {
    return null;
  }

  @Override
  public void fromJson(JSONObject json) {
    for(Field field:this.getClass().getDeclaredFields()) {
      field.setAccessible(true);
      try {
        if(json.containsKey(field.getName())) {
          Object value = field.get(this);
          field.set(this,json.get(field.getName()));
        }

      } catch (IllegalAccessException e) {

      }
    }
  }

  @Override
  public JSONObject toJson() {
    JSONObject json = new JSONObject();
    for(Field field:this.getClass().getDeclaredFields()) {
      field.setAccessible(true);
      try {
        Object value = field.get(this);
        if(value!=null)
          json.put(field.getName(),value);
      } catch (IllegalAccessException e) {
        json.remove(field.getName());
      }
    }
    return json;
  }
  public void setAllValue(Row row) {
	// TODO Auto-generated method stub

	  Cell cell = row.getCell(0);
      String cellValue = this.getCellValue(cell);
      this.publicationNumber=cellValue;

      cell = row.getCell(1);
      cellValue = this.getCellValue(cell);
      this.applicationNumber = cellValue;

      cell = row.getCell(2);
      cellValue = this.getCellValue(cell);
      this.applicationYear = cellValue;

      cell = row.getCell(3);
      cellValue = this.getCellValue(cell);
      this.patentRype = cellValue;

      cell = row.getCell(4);
      cellValue = this.getCellValue(cell);
      this.legalStatus = cellValue;

      cell = row.getCell(5);
      cellValue = this.getCellValue(cell);
      this.applicant = cellValue;

      cell = row.getCell(6);
      cellValue = this.getCellValue(cell);
      this.proposer = cellValue;

      cell = row.getCell(7);
      cellValue = this.getCellValue(cell);
      this.currentApplicant= cellValue;

      cell = row.getCell(8);
      cellValue = this.getCellValue(cell);
      this.patentName = cellValue;

      cell = row.getCell(9);
      cellValue = this.getCellValue(cell);
      this.IPC = cellValue;

      cell = row.getCell(10);
      cellValue = this.getCellValue(cell);
      this.applicationData=cellValue;

      cell = row.getCell(11);
      cellValue = this.getCellValue(cell);
      this.openYear=cellValue;

      cell = row.getCell(12);
      cellValue = this.getCellValue(cell);
      this.openDay=cellValue;

      cell = row.getCell(13);
      cellValue = this.getCellValue(cell);
      this.patentAbstract=cellValue;//N

      cell = row.getCell(14);
      cellValue = this.getCellValue(cell);
      this.independentClaims=cellValue;

      cell = row.getCell(15);
      cellValue = this.getCellValue(cell);
      this.primaryClaim =cellValue;

      cell = row.getCell(16);
      cellValue = this.getCellValue(cell);
      this.claim=cellValue;

      cell = row.getCell(17);
      cellValue = this.getCellValue(cell);
      this.INPADOC =cellValue;

      cell = row.getCell(18);
      cellValue = this.getCellValue(cell);
      this.simpleLegalState  =cellValue;

      cell = row.getCell(19);
      cellValue = this.getCellValue(cell);
      this.legalStatusSate=cellValue;

      cell = row.getCell(20);
      cellValue = this.getCellValue(cell);
      this.inventor =cellValue;

      cell = row.getCell(21);
      cellValue = this.getCellValue(cell);
      this.theInventor=cellValue;

      cell = row.getCell(22);
      cellValue = this.getCellValue(cell);
      this.firstInventor=cellValue;

      cell = row.getCell(23);
      cellValue = this.getCellValue(cell);
      this.inventorCount=cellValue;

      cell = row.getCell(24);
      cellValue = this.getCellValue(cell);
      this.applicantCount =cellValue;

      cell = row.getCell(25);
      cellValue = this.getCellValue(cell);
      this.applicanProvince  =cellValue;

      cell = row.getCell(26);
      cellValue = this.getCellValue(cell);
      this.INPADOCCount=cellValue;

      cell = row.getCell(27);
      cellValue = this.getCellValue(cell);
      this.patentCount=cellValue;

      cell = row.getCell(28);
      cellValue = this.getCellValue(cell);
      this.INPADOCPatent  =cellValue;

      cell = row.getCell(29);
      cellValue = this.getCellValue(cell);
      this.patentFamily =cellValue;

      cell = row.getCell(30);
      cellValue = this.getCellValue(cell);
      this.citationPatternCount=cellValue;

      cell = row.getCell(31);
      cellValue = this.getCellValue(cell);
      this.documentNotPatternCount=cellValue;

      cell = row.getCell(32);
      cellValue = this.getCellValue(cell);
      this.beCitedPatternCount=cellValue;

      cell = row.getCell(33);
      cellValue = this.getCellValue(cell);
      this.citePattern=cellValue;

      cell = row.getCell(34);
      cellValue = this.getCellValue(cell);
      this.INPADOCCitedTotal=cellValue;

      cell = row.getCell(35);
      cellValue = this.getCellValue(cell);
      this.citedTotal =cellValue;

      cell = row.getCell(36);
      cellValue = this.getCellValue(cell);
      this.citedTotalOneYear=cellValue;

      cell = row.getCell(37);
      cellValue = this.getCellValue(cell);
      this.citedTotalFiveYear=cellValue;

      cell = row.getCell(38);
      cellValue = this.getCellValue(cell);
      this.citedPatent  =cellValue;

      cell = row.getCell(39);
      cellValue = this.getCellValue(cell);
      this.IPCMainNumber=cellValue;

      cell = row.getCell(40);
      cellValue = this.getCellValue(cell);
      this.IPCMainNumberBigC=cellValue;

      cell = row.getCell(41);
      cellValue = this.getCellValue(cell);
      this.IPCMainNumberSmallC=cellValue;

      cell = row.getCell(42);
      cellValue = this.getCellValue(cell);
      this.IPCMainNumberBigO=cellValue;

      cell = row.getCell(43);
      cellValue = this.getCellValue(cell);
      this.priorityCountry =cellValue;

      cell = row.getCell(44);
      cellValue = this.getCellValue(cell);
      this.priorityNumber =cellValue;

      cell = row.getCell(45);
      cellValue = this.getCellValue(cell);
      this.priorityData =cellValue;
  }
  private String getCellValue(Cell cell) {
      String str = null;



    switch (cell.getCellType()) {
      case Cell.CELL_TYPE_BLANK:
        str = "";
        break;
      case Cell.CELL_TYPE_BOOLEAN:
        str = String.valueOf(cell.getBooleanCellValue());
        break;
      case Cell.CELL_TYPE_FORMULA:
        str = String.valueOf(cell.getCellFormula());
        break;
      case Cell.CELL_TYPE_NUMERIC:
        str = String.valueOf(cell.getNumericCellValue());
        break;
      case Cell.CELL_TYPE_STRING:
        str = String.valueOf(cell.getStringCellValue());

        break;
      default:
        str = null;
        break;
    }

    if(cell.getColumnIndex() == 16) {
      String[] tmp = str.split("\"");
      if(tmp.length > 3) {
        str = tmp[3];
      } else str = null;
    }

    return str;
  }

  @Override
  public <T> T getField(String name) {
    if(name.equals("claim")) {
      if(claim == null) return (T) primaryClaim;
      try {
        File file = new File(claimPath + "/"+claim.trim());
        String res = IOUtils.toString(file.toURI(), "UTF-8");
        return (T) res;
      } catch (IOException e) {
        e.printStackTrace();
        return (T) primaryClaim;
      }
    }

    return Documentable.super.getField(name);
  }
}
